﻿define([
    'dojo/_base/declare',
    'esri/geometry/Point',
    'geomap/plugins/layers/DivsClusterLayer',
    'geomap/plugins/Div',
], function (declare, Point, BaseDivsLayer, Div) {
    var DivsClusterLayer = declare([BaseDivsLayer], {
        setData: function (mapView, layer) {
            var me = this;
            var mapUtils = mapView.mapUtils;
            var options = layer.layer || {};
            var data = $.extend(true, [], options.data);
            var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
            var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();

            lngLatArray.map(function (lngLat, index) {
                var point = mapUtils.createPoint(mapView, lngLat);
                var symbol = lngLat[symbolFieldName] || options.symbol || {};
                var div = new Div(point);

                $(div.getElement())
                    .addClass(symbol.class)
                    .css(symbol.css || {})
                    .html(symbol.html);
                div.attributes = lngLat;
                me.add(div);
                if (index == lngLatArray.length - 1) {
                    if (typeof options.callback == 'function') {
                        options.callback(layer);
                    }
                }
            });
        },
        getGraphicLocation: function (mapView, esriEvent) {
            var geometry = esriEvent.graphic.geometry;
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        },
    });
    return DivsClusterLayer;
});
